package com.lty.jeeg.server.modules.tms.controller;

import com.lty.jeeg.server.common.dto.ResponseInfo;
import com.lty.jeeg.server.common.enums.ResponseEnum;
import com.lty.jeeg.server.common.utils.ResponseUtil;
import com.lty.jeeg.server.modules.tms.model.Task;
import com.lty.jeeg.server.modules.tms.service.TaskService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lty.jeeg.server.common.page.table.PageTableRequest;
import com.lty.jeeg.server.common.page.table.PageTableResponse;


import io.swagger.annotations.ApiOperation;

/**
 * @author : LTY
 * @date : 2019-03-26 10:53:09
 * Description :
 */
@Api(tags = "任务")
@RestController
@RequestMapping("/task")
public class TaskController {

    private final TaskService taskService;

    @Autowired
    public TaskController(TaskService taskService) {
        this.taskService = taskService;
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取")
    public ResponseInfo get(@PathVariable Long id) {
        Task byId = taskService.getById(id);
        return ResponseUtil.success(byId);
    }


    @RequestMapping("/{projectId}")
    @ApiOperation(value = "根据projectId获取")
    public ResponseInfo getList(@PathVariable Long projectId) {
        return ResponseUtil.success(taskService.getByProjectId(projectId));
    }

    @PostMapping
    @ApiOperation(value = "保存")
    public ResponseInfo save(@RequestBody Task task) {
        return ResponseUtil.success(taskService.save(task));
    }

    @PutMapping
    @ApiOperation(value = "修改")
    public ResponseInfo update(@RequestBody Task task) {
        return ResponseUtil.success(taskService.update(task));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public ResponseInfo delete(@PathVariable Long id) {
        return ResponseUtil.success(taskService.delete(id));
    }

    /**
     * 修改任务状态
     *
     * @param taskAfter 修改后的
     * @return
     */
    @PostMapping("/updateStatus")
    @ApiOperation(value = "修改任务状态")
    public ResponseInfo updateStatus(@RequestBody Task taskAfter) {
        Task taskBefore = taskService.getById(taskAfter.getId());
        if (taskBefore == null) {
            return ResponseUtil.error(ResponseEnum.TASK_NOT_FOUND);
        } else {
            if (!taskBefore.getStatus().equals(taskAfter.getStatus())) {
                //修改任务状态
                taskService.changeStatus(taskAfter.getId(), taskAfter.getStatus());
            }
            return ResponseUtil.success(ResponseEnum.SUCCESS);
        }
    }
    /**
     * 修改任务优先级
     *
     * @param taskAfter 修改后的
     * @return
     */
    @PostMapping("/updateGrade")
    @ApiOperation(value = "修改任务优先级")
    public ResponseInfo updateGrade(@RequestBody Task taskAfter) {
        Task taskBefore = taskService.getById(taskAfter.getId());
        if (taskBefore == null) {
            return ResponseUtil.error(ResponseEnum.TASK_NOT_FOUND);
        } else {
            if (!taskBefore.getGrade().equals(taskAfter.getGrade())) {
                //修改任务优先级
                taskService.changeGrade(taskAfter.getId(), taskAfter.getGrade());
            }
            return ResponseUtil.success(ResponseEnum.SUCCESS);
        }
    }

    @PostMapping("/updateParticipant")
    @ApiOperation(value = "修改参与人")
    public ResponseInfo updateParticipant(@RequestBody Task taskAfter) {
        Task taskBefore = taskService.getById(taskAfter.getId());
        if (taskBefore == null) {
            return ResponseUtil.error(ResponseEnum.TASK_NOT_FOUND);
        } else {
            if (!taskBefore.getParticipantId().equals(taskAfter.getParticipantId())) {
                //修改参与人
                taskService.changeParticipant(taskAfter.getId(), taskAfter.getParticipantId());
            }
            return ResponseUtil.success(ResponseEnum.SUCCESS);
        }
    }

    @PostMapping("/updateExecutor")
    @ApiOperation(value = "修改负责人")
    public ResponseInfo updateExecutor(@RequestBody Task taskAfter) {
        Task taskBefore = taskService.getById(taskAfter.getId());
        if (taskBefore == null) {
            return ResponseUtil.error(ResponseEnum.TASK_NOT_FOUND);
        } else {
            if (!taskBefore.getExecutorId().equals(taskAfter.getExecutorId())) {
                //修改负责人
                taskService.changeExecutor(taskAfter.getId(), taskAfter.getExecutorId());
            }
            return ResponseUtil.success(ResponseEnum.SUCCESS);
        }
    }

    @PostMapping("/updateActualTotalWorkingHours")
    @ApiOperation(value = "登记工时")
    public ResponseInfo updateActualTotalWorkingHours(@RequestBody Task taskAfter) {
        Task task = taskService.getById(taskAfter.getId());
        if (task == null) {
            return ResponseUtil.error(ResponseEnum.TASK_NOT_FOUND);
        } else {
            taskService.changeActualTotalWorkingHours(taskAfter.getId(), taskAfter.getActualTotalWorkingHours());
            return ResponseUtil.success(ResponseEnum.SUCCESS);
        }
    }

    @PostMapping("/updateEstimateWorkingHours")
    @ApiOperation(value = "修改预计工时")
    public ResponseInfo updateEstimateWorkingHours(@RequestBody Task taskAfter) {
        Task taskBefore = taskService.getById(taskAfter.getId());
        if (taskBefore == null) {
            return ResponseUtil.error(ResponseEnum.TASK_NOT_FOUND);
        } else {
            if (!taskBefore.getEstimateWorkingHours().equals(taskAfter.getEstimateWorkingHours())) {
                //修改预计工时
                taskService.changeEstimateWorkingHours(taskAfter.getId(), taskAfter.getEstimateWorkingHours());
            }
            return ResponseUtil.success(ResponseEnum.SUCCESS);
        }
    }
    @PostMapping("/updateDeadline")
    @ApiOperation(value = "修改截止日期")
    public ResponseInfo updateDeadline(@RequestBody Task taskAfter) {
        Task taskBefore = taskService.getById(taskAfter.getId());
        if (taskBefore == null) {
            return ResponseUtil.error(ResponseEnum.TASK_NOT_FOUND);
        } else {
            if (!taskBefore.getDeadline().equals(taskAfter.getDeadline())) {
                //修改截止日期
                taskService.changeDeadline(taskAfter.getId(), taskAfter.getDeadline());
            }
            return ResponseUtil.success(ResponseEnum.SUCCESS);
        }
    }

    @PostMapping("/updateConjunctionTask")
    @ApiOperation(value = "修改关联任务")
    public ResponseInfo updateConjunctionTask(@RequestBody Task taskAfter) {
        Task taskBefore = taskService.getById(taskAfter.getId());
        if (taskBefore == null) {
            return ResponseUtil.error(ResponseEnum.TASK_NOT_FOUND);
        } else {
            taskService.changeConjunctionTask(taskAfter.getId(), taskAfter.getConjunctionTaskId(),taskAfter.getConjunctionTaskName());
            return ResponseUtil.success(ResponseEnum.SUCCESS);
        }
    }

    @GetMapping("/list")
    @ApiOperation(value = "列表")
    public PageTableResponse list(PageTableRequest request) {
        return taskService.list(request);
    }

    /**
     * 更改任务是否置顶
     *
     * @param isTop 是否置顶
     * @return
     */
    @PostMapping("/updateIsTop/{taskId}/{isTop}")
    @ApiOperation(value = "修改是否置顶")
    public ResponseInfo updateIsTop(@PathVariable("taskId") Long taskId, @PathVariable("isTop") Integer isTop) {
            return ResponseUtil.success(taskService.updateIsTop(taskId, isTop));
    }
}
